package cn.codeforfun;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class ChronomancerI18nApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChronomancerI18nApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(
            @Value("${chronomancer.source.path}") String sourcePath,
            @Value("${chronomancer.config.path}") String configPath,
            @Value("${chronomancer.config.language}") String language
    ) {
        return args -> {
            System.out.println(sourcePath);
            System.out.println(configPath);
            System.out.println(language);

            String config = FileUtil.readString(configPath + File.separator + "config.json", StandardCharsets.UTF_8);
            Map<String, Object> configMap = JsonUtils.toMap(config);
            String translateConfig = FileUtil.readString(configPath + File.separator + "config-" + language + ".json", StandardCharsets.UTF_8);
            Map<String, Object> translateConfigMap = JsonUtils.toMap(translateConfig);

            File folder = new File(sourcePath);
            List<File> files = listFile(folder);

            replaceFile(files, configMap, translateConfigMap);
        };
    }

    private void replaceFile(List<File> files, Map<String, Object> configMap, Map<String, Object> translateConfigMap) {
        for (File file : files) {
            String s = FileUtil.readString(file, StandardCharsets.UTF_8);
            for (Map.Entry<String, Object> c : configMap.entrySet()) {
                String s1 = c.getValue().toString();
                if (s.contains(s1)) {
                    String s2 = ReUtil.replaceAll(s, s1, translateConfigMap.get(c.getKey()).toString());
                    FileUtil.writeString(s2, file, StandardCharsets.UTF_8);
                }
            }
        }
    }

    private List<File> listFile(File file) {
        List<File> result = new ArrayList<>();
        if (".git".equals(file.getName())
                || ".vscode".equals(file.getName())
                || "pubspec.lock".equals(file.getName())
                || "README.md".equals(file.getName())
                || "pubspec.yaml".equals(file.getName())
                || "analysis_options.yaml".equals(file.getName())
                || ".vscode".equals(file.getName())
                || ".idea".equals(file.getName())
                || "Dockerfile".equals(file.getName())
                || ".gitignore".equals(file.getName())
                || ".dockerignore".equals(file.getName())
                || file.getName().endsWith(".py")
                || file.getName().endsWith(".png")
                || file.getName().endsWith(".woff")
                || file.getName().endsWith(".css")
        ) {
            return result;
        }
        File[] listFiles = file.listFiles();
        if (listFiles == null) {
            result.add(file);
        } else {
            for (File f : listFiles) {
                result.addAll(listFile(f));
            }
        }
        return result;
    }
}
